package com.mvwchina.common.utils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 */
@Component
public class RedisUtils {
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private ValueOperations<String, String> valueOperations;
	@Autowired
	private HashOperations<String, String, Object> hashOperations;
	@Autowired
	private ListOperations<String, Object> listOperations;
	@Autowired
	private SetOperations<String, Object> setOperations;
	@Autowired
	private ZSetOperations<String, Object> zSetOperations;
	/** 默认过期时长，单位：秒 */
	public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
	/** 不设置过期时长 */
	public final static long NOT_EXPIRE = -1;

	public void setHash(String key,String hashKey,Object obj,long expire){
		hashOperations.put(key,hashKey,toJson(obj));
		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
	}

	public void setHash(String key,String hashKey,Object obj){
		setHash(key,hashKey,obj,DEFAULT_EXPIRE);
	}

	public String getHash(String key,String hashKey){
		String value = hashOperations.get(key,hashKey)==null?"":hashOperations.get(key,hashKey).toString();
		return value;
	}

	public <T> T getHash(String key,String hashKey,Class<T> clazz){
		String value = hashOperations.get(key,hashKey)==null?"":hashOperations.get(key,hashKey).toString();
		return value == null ? null : fromJson(value, clazz);
	}

	/**
	 * 缓存一个集合
	 *
	 * @param key          键
	 * @param collecation  要缓存的集合
	 * @param elementClass 集合中类的类型
	 */
	public <T> void setList(String key, Collection<T> collecation, Class<?> elementClass,long expire) {
		//无论数据是否为空，都推一个key到redis，防止数据为null时不走缓存
		listOperations.getOperations().delete(key);
		List<T> list = (List<T>) collecation;

		for (T t:list){
			listOperations.leftPush(key,toJson(t));
		}

		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
	}

	public <T> void setList(String key, Collection<T> collecation, Class<?> elementClass){
		setList(key,collecation,elementClass,DEFAULT_EXPIRE);
	}

	public void setListOfOne(String key,Object obj){
		listOperations.leftPush(key,obj);
	}

	public boolean hasKey(String key) {
		return redisTemplate.hasKey(key);
	}

	public boolean hasHashKey(String key,String hashKey) {
		return hashOperations.hasKey(key, hashKey);
	}

	public <T extends Collection<E>, E> T getList(String key, Class<T> collectionClass, Class<E> elementClass) {
		if (!hasKey(key)) {
			return null;
		}

		T conllection = (T) Lists.newArrayList();

		List<Object> collecationJson = listOperations.range(key, 0, -1);
		if (collecationJson == null || collecationJson.isEmpty()) {
			return conllection;
		}

		for (int i = 0; i < collecationJson.size(); i++) {
			conllection.add(fromJson(collecationJson.get(i).toString(), elementClass));
		}

		return conllection;
	}

	public void set(String key, Object value, long expire) {
		valueOperations.set(key, toJson(value));
		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
	}

	public void set(String key, Object value) {
		set(key, value, DEFAULT_EXPIRE);
	}

	public <T> T get(String key, Class<T> clazz, long expire) {
		String value = valueOperations.get(key);
		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
		return value == null ? null : fromJson(value, clazz);
	}

	public <T> T get(String key, Class<T> clazz) {
		return get(key, clazz, NOT_EXPIRE);
	}

	public String get(String key, long expire) {
		String value = valueOperations.get(key);
		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
		return value;
	}

	public String get(String key) {
		return get(key, NOT_EXPIRE);
	}

	public void delete(String key) {
		redisTemplate.delete(key);
	}

	/**
	 * Object转成JSON数据
	 */
	private String toJson(Object object) {
		if (object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double || object instanceof Boolean || object instanceof String) {
			return String.valueOf(object);
		}
		return JSON.toJSONString(object);
	}

	/**
	 * JSON数据，转成Object
	 */
	private <T> T fromJson(String json, Class<T> clazz) {
		return JSON.parseObject(json, clazz);
	}
}
